package Exercise;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Properties;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Exercise03 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader("src\\dog.properties"));
        String name = properties.getProperty("name");
        int age = Integer.parseInt(properties.getProperty("age"));
        String color = properties.getProperty("color");

        Dog dog = new Dog(name,age,color);
        System.out.println(dog);

        //将dog对象序列化到文件“e:\\dog.dat”中
        //如果要对象时可序列化的，则必须要实现Serializable接口
        String filePath = "e:\\\\dog.txt";
        ObjectOutputStream oos  = new ObjectOutputStream(new FileOutputStream(filePath));
        oos.writeObject(dog);
        oos.close();

    }
    //反序列化dog信息
    @Test
    public void f1() throws IOException, ClassNotFoundException {
        String filePath = "e:\\dog.txt";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
        Object dog = ois.readObject();
        System.out.println(dog);
        ois.close();
    }
}
class Dog implements Serializable{
    private String name;
    private int age;
    private String color;

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}
